$(() => {
  // 获取页面详情信息
  chrome.extension.onMessage.addListener(
    function (req, sender, res) {
      if (req.action === 'completed') {
        var pageInfo = {}
        if (/http:\/\/www.youboy\.com\/s.+\.html/.test(req.url)) {
          // b2b PC端
          pageInfo.image1 = $('.view-big-image img').attr('src')
          pageInfo.location = $('.product-footer-copy p:first-child').text().split('地址：')[1]
          pageInfo.pcHomepage = $('.product-detail-menu-link a:first-child').attr('href')
          pageInfo.searchPcUrl = pageInfo.pcHomepage
          let price = parseFloat($('.price-swipe1').text().replace(/[^0-9\.]/ig, ''))
          if (Number.isNaN(price)) {
            inputPrice('请输入价格', function(val, index) {
              if (/^((0{1}\.\d{1,2})|([1-9]\d*\.{1}\d{1,2})|([1-9]+\d*))$/.test(val)) { 
                pageInfo.price = val
                // 跳转去移动端
                sendMsg(pageInfo, 'set-keyword-data')
                layer.close(index)
              } else {
                layer.msg('格式不正确')
              }
            })
          } else {
            pageInfo.price = price.toFixed(2)
            sendMsg(pageInfo, 'set-keyword-data')
          }
        } else if (/https:\/\/m\.youboy\.com\/p.+\.html/.test(req.url)) {
          // b2b 移动端
          // 店铺首页
          pageInfo.homepage = location.origin + $('.detail-product-shop-share a:nth-child(2)').attr('href')
          pageInfo.searchWiseUrl = location.origin + $('.detail-product-shop-share a:nth-child(1)').attr('href')
          sendMsg(pageInfo, 'get-keyword-next')
        } else if (/http:\/\/www\.youboy\.com\/mall\/product\/.+/.test(req.url)) {
          // 直卖网 PC端
          pageInfo.image1 = $('#ma-smPic img').attr('src')
          let pcHomepage = location.origin + $('.nav_list:first-child').attr('href')
          let address = $('.factory-desc').text().trim()
          let price = $('.price-number').text().trim()
          let price2 = $('.purchase-item-price:first-child').text().trim()
          
          pageInfo.pcHomepage = pcHomepage
          pageInfo.homepage = pcHomepage.replace('http://www.youboy.com/mall/factory/', 'https://gyp.youboy.com/h5/shop/index?factoryId=')
          pageInfo.searchPcUrl = pcHomepage.replace('/factory/', '/product/list/')
          pageInfo.searchWiseUrl = pageInfo.homepage
          if (address) {
            pageInfo.location = address
            getNext(price, price2, pageInfo)
          } else {
            inputPrice('请输入地址', function(val, index) {
              pageInfo.location = val
              getNext(price, price2, pageInfo)
              layer.close(index)
            })
          }
        }
      } else if (req.action === 'setTagUrl') {
        location.href = req.url
      }
    }
  )
})

function getNext(price, price2, pageInfo) {
  if (price || price2) {
    price ? pageInfo.price = price : pageInfo.price = parseFloat(price2.replace(/[^0-9\.]/ig, '')).toFixed(2)
    sendMsg(pageInfo, 'get-keyword-next', )
  } else {
    inputPrice('请输入价格', function(val) {
      if (/^((0{1}\.\d{1,2})|([1-9]\d*\.{1}\d{1,2})|([1-9]+\d*))$/.test(val)) { 
        pageInfo.price = val
        // 跳转去移动端
        sendMsg(pageInfo, 'get-keyword-next')
        layer.close(index)
      } else {
        layer.msg('格式不正确')
      }
    })
  }
}

function inputPrice(title, callback) {
  layer.prompt({
    title: title,
    formType: 3,
    closeBtn:false,
    btn2: function() {
      inputPrice(callback)
    }
  }, function(pass, index) {
    callback(pass, index)
  })
}

// 将获取内容传递给后台脚本进行处理
function sendMsg(msg, cmd) {
　chrome.extension.sendMessage({ 'msg': msg, 'cmd': cmd })
}
